﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using FCNS.Utility;

using FCNS.KeepMoney.Util;
using FCNS.KeepMoneyLibrary.Data;
using FCNS.KeepMoneyLibrary.Util;

namespace FCNS.KeepMoney.Forms
{
    public partial class EntryForm : ParentForm
    {
        EntryField editEntryField = null;
        List<BankField> bankField = BankField.GetBankField.FindAll(
            (delegate(BankField bf) { return bf.Enable; }));

        List<GroupField> groupField = GroupField.GetGroupField;

        public EntryForm()
        {
            InitializeComponent();

            BannerFactory.CreateBannerEx(this, pictureBoxLogo, Properties.Resources.m1_32,
             LanguageUtil.账单记录, LanguageUtil.你可以在这里记录收入与支出的明细);

            numericUpDownMoney.DecimalPlaces = Program.AppConfig.Money.DecimalPlaces;

            foreach (BankField bf in bankField)
                comboBoxPurse.Items.Add(bf.Name);

            comboBoxGroup.Items.AddRange(GroupField.GetGroupNames(groupField));

            Program.Translation.ApplyTo(this);
        }

        /// <summary>
        /// 编辑已有行
        /// </summary>
        /// <param name="field">已选择的账务记录行</param>
        public void Init(EntryField field)
        {
            editEntryField = field;
            dateTimePickerDate.Value = field.Date;
            textBoxName.Text = field.columnName;
            numericUpDownMoney.Value = field.Money;
            textBoxNote.Text = field.columnNote;

            comboBoxPurse.SelectedIndex = bankField.IndexOf(bankField.Find(bf=>{return bf.Id == field.BankId;}));

            if (field.AlertDate is DateTime)
            {
                checkBoxAlert.Checked = true;
                dateTimePickerAlert.Value = (DateTime)field.AlertDate;
            }

            comboBoxGroup.SelectedIndex = groupField.Find(gf=>{return gf.Id == field.GroupId;}).Index;
        }
        /// <summary>
        /// 新建行
        /// </summary>
        public void Init(GroupField gField)
        {
            comboBoxGroup.SelectedIndex = gField.Index;

            if (string.IsNullOrEmpty(comboBoxGroup.Text))
                comboBoxGroup.SelectedIndex = 0;

            comboBoxPurse.SelectedIndex = bankField.IndexOf(bankField.Find(bf => { return bf.Default; }));
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            if (numericUpDownMoney.Value == 0)
                return;

            BankField newBank = bankField[comboBoxPurse.SelectedIndex];
            GroupField gField = groupField.Find(gf=>{return gf.Index == comboBoxGroup.SelectedIndex;});

            if (editEntryField == null)
            {
                if (!gField.IorE)
                {
                    if (newBank.Balance + newBank.Credit - numericUpDownMoney.Value < 0)
                    {
                        MessageBox.Show(LanguageUtil.余额不足);
                        return;
                    }
                }

                editEntryField = new EntryField( gField.Id, newBank.Id,  dateTimePickerDate.Value, numericUpDownMoney.Value, KMDEntryType.Normal);

                if (editEntryField.Row == null)
                    editEntryField = null;
                else
                {
                    editEntryField.AddRowToDataTable();
                    EditEntry();
                }
            }
            else
            {
                decimal oldMoney = editEntryField.Money;

                BankField oldBank = bankField.Find(bf=> { return bf.Id == editEntryField.BankId; });

                if (newBank.Id == editEntryField.BankId)
                {
                    if (editEntryField.Money<0)
                        if (oldBank.Balance + oldBank.Credit + numericUpDownMoney.Value - oldMoney < 0)
                        {
                            MessageBox.Show(LanguageUtil.余额不足);
                            return;
                        }

                    EditEntry();
                    oldBank.Balance += ( numericUpDownMoney.Value - oldMoney);
                }
                else//更改了钱袋
                {
                    if (oldBank.Balance - oldMoney < 0)
                    {
                        MessageBox.Show(LanguageUtil.余额不足);
                        return;
                    }

                    if (!gField.IorE)
                    {
                        if (newBank.Balance - numericUpDownMoney.Value < 0)
                        {
                            MessageBox.Show(LanguageUtil.余额不足);
                            return;
                        }
                    }

                    oldBank.Balance = oldBank.Balance - oldMoney;
                    newBank.Balance = newBank.Balance + numericUpDownMoney.Value;

                    EditEntry();
                }
            }

            this.DialogResult = DialogResult.OK;
        }

        private void EditEntry()
        {
            GroupField gField = groupField.Find(gf=>{ return gf.Index == comboBoxGroup.SelectedIndex; });

            editEntryField.GroupId = gField.Id;
            editEntryField.columnName = textBoxName.Text;
            editEntryField.Money = numericUpDownMoney.Value;
            editEntryField.Date = dateTimePickerDate.Value;
            editEntryField.columnNote = textBoxNote.Text;

            if (checkBoxAlert.Checked)
                editEntryField.AlertDate = dateTimePickerAlert.Value;
            else
                editEntryField.AlertDate =null;

            editEntryField.BankId = bankField[comboBoxPurse.SelectedIndex].Id;
        }

        private void OnShowCalcForm(object sender, EventArgs e)
        {
            CalcForm cf = new CalcForm();
            cf.Show(this);
        }

        public decimal Value
        {
            set
            { numericUpDownMoney.Value = value; }
        }

        public EntryField GetField
        {
            get { return editEntryField; }
        }
    }
}
